'use strict'
var info = require('./Info')
// info();
var Crawler = require("crawler");

var table=require('./Table')
var fs = require('fs');//创建
var request = require('request');//储存


var c = new Crawler({
    maxConnections: 10,
    callback: function (error, res, done) {
        if (error) {
            console.log(error);
        } else {
            var $ = res.$;
            // var list = $('#content .article table').toArray();
            // list.forEach(element => {
            //     var img = $(element).find('img').attr('src')

            //     var a = img.split('/').slice(7, 9).toString();
            //     request(img).pipe(fs.createWriteStream('./picture/' + a + ".jpg"))

            //     // console.log(img);
            // });

                      var list = $('#content .article table').toArray();

                        list.forEach(element => {
                            var txt1 = $(element).find('.pl2').text().trim().replace(/[ \n\r]/g, "");
                            var txt2 = $(element).find('.pl').text().trim().replace(/[ \n\r]/g, "");
                            var txt3 = $(element).find('.quote').text().trim().replace(/[ \n\r]/g, "");

                            var a = txt2.split('/').slice(0,1).toString();
                            table.create({bookName:txt1,author:a,introduce:txt3})
                        });
                        // $ is Cheerio by default
                        //a lean implementation of core jQuery designed specifically for the server

            done();

        }
    }
});

// Queue just one URL, with default callback
for (var index = 0; index <= 220; index += 25) {
    var URL = 'https://book.douban.com/top250?start=' + index
    c.queue(URL);
}